<template>
  <div>
    <h3>视频上传</h3>
    <div class="box">
      <el-upload
        class="avatar2-uploader"
        action="#"
        :http-request="headUpload"
      >
        <img v-if="driverPhoto" :src="driverPhoto" class="avatar2">
        <i v-else class="el-icon-plus avatar2-uploader-icon" />
      </el-upload>
      <div class="bnt">
        <el-button type="primary" style="padding:8px" icon="el-icon-upload" @click="upvideo">视频上传</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { headUpload, upvideos, upvideolog } from '@/api/activityCharge'

export default {
  data() {
    return {
      driverPhoto: '' // 视频地址
    }
  },
  methods: {
    // 视频上传的方法
    async headUpload(file) {
      var formData = new FormData()
      formData.append('file', file.file)
      const result = await headUpload(formData)
      console.log(result.data)
      this.driverPhoto = result.data
    },
    upvideo() {
      if (this.driverPhoto === '') {
        this.$message.error('请上传视频')
      } else if (this.driverPhoto.includes('mp4') === false) {
        this.$message.error('请上传正确视频格式')
      } else {
        upvideos(this.driverPhoto).then(res => {
          console.log(res)
          this.$message({
            message: '图片上传成功',
            type: 'success'
          })
          this.$router.go(0)
          // 修改操作日志
          upvideolog().then(res => {
            console.log(res)
          })
        })
      }
    }
  }

}
</script>

<style scoped>
*{
    padding: 0;
    margin: 0;
}
h3{
  margin-left: 5%;
  margin-top: 2%;
}
.box{
    width: 601px;
    height: 300px;
    margin: 0 auto;
    margin-top: 5%;
    border-radius: 12px;
    border: 1px solid #eee;
}
.avatar2-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
}
.avatar2-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar2-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 600px;
  height: 300px;
  line-height: 300px;
  text-align: center;
  border: 1px solid rgb(220, 223, 230);
  border-radius: 10px;
}
.avatar2 {
  width: 178px;
  height: 178px;
  display: block;
}
.bnt{
  float: right;
  margin-top: 20px;
}
</style>